package main

import (
	"fmt"
	"io"
	"log"
	url2 "net/url"
	"os"
	"regexp"
	"strconv"
	"time"

	"gitee.com/yangangao/gadgets/wbSpider/excel"
)

var (
	Info  *log.Logger
	Debug *log.Logger
)

func init() {
	rfile, err := os.OpenFile("./running.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		log.Fatalln("Faild to open running logger file:", err)
	}
	Info = log.New(io.MultiWriter(rfile, os.Stderr), "INFO  : ", log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile)

	if debug {
		dfile, err := os.OpenFile("./debug.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
		if err != nil {
			log.Fatalln("Faild to open running logger file:", err)
		}
		Debug = log.New(io.MultiWriter(dfile, os.Stderr), "DEBUG : ", log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile)
	}
}

var (
	// filename    string
	// username string
	// uid      string
	// keywordlist []string
	itemcount int
)

func main() {
	if debug {
		Debug.Println("get cookie", cookie[:20]+"..."+cookie[len(cookie)-20:])
		Debug.Println("get todolist", todolist)
		Debug.Println("get keyword list", keywordlist)
	} else {
		fmt.Println("debug if off")
	}

	t := time.Now()
	t_limit := time.Date(2022, 11, 4, 0, 0, 0, 0, t.Location())
	if time.Now().Sub(t_limit) > 0 {
		fmt.Println("TIMEOUT, PLEASE CONTRACT ADMIN!")
		time.Sleep(1 * time.Hour)
		return
	}

	// username = "人民网"
	// uid = "2286908003"
	// keywordlist = []string{"女博士", "女大学生", "女硕士", "女研究生"}
	// pagelimit = 30
	itemcount = 0

	r_mblogid := regexp.MustCompile(`"mblogid":"(.*?)","user"`)
	r_isLongText := regexp.MustCompile(`"isLongText":(.*?),"`)
	r_text_raw := regexp.MustCompile(`"text_raw":"(.*?)","`)
	r_created_at := regexp.MustCompile(`"created_at":"(.*?)"`)

	excel.Create(filename,
		[]string{
			"序号", "是否长文本", "关键字", "用户名", "发布时间", "内容",
		})

	for username, uid := range todolist {
		if debug {
			Debug.Println(" \n现在准备对", username, uid, "进行查找\n ")
		}

		for keywordcount, keyword := range keywordlist {
			Info.Println("获取关键字信息：", keyword, keywordcount)

			urlkeyword := url2.QueryEscape(keyword)
			for page := 0; ; {
				page++
				if page > pagelimit {
					fmt.Println("all pages are done!")
					break
				}

				url := `https://weibo.com/ajax/profile/searchblog?uid=` + uid + `&page=` + strconv.Itoa(page) + `&feature=0&q=` + urlkeyword
				result, err := WBHttpGet(url)
				if err != nil {
					fmt.Println(err.Error())
				}

				mblogid, isLongText, text_raw, created_at, length := rawMsgHandler(result, r_mblogid, r_isLongText, r_text_raw, r_created_at)

				Info.Println("在第", page, "页发现了", length, "条数据，目前已有数据条数：", itemcount)
				if length == 0 {
					Info.Println("怀疑当前已无更多数据，结束进程。")
					if debug {
						Debug.Println(result)
					}
					break
				}

				getWeiboLongText(mblogid, isLongText, created_at, filename, username, keyword, itemcount)
				getRawText(text_raw, isLongText, created_at, filename, username, keyword, itemcount)
				itemcount += length
			}
		}
		// break
		itemcount = 0
	}

	Info.Println("ALL DONE!  please close this window and check you result in:", filename+".xlsx")
	time.Sleep(1 * time.Hour)
}
